//
//  ECMemberCommonSelectViewController.m
//  ECERP.CRM
//
//  Created by 刘思麟 on 2018/11/4.
//  Copyright © 2018 Mac. All rights reserved.
//

#import "ECMemberCommonSelectViewController.h"
#import "ECMemberAddTableViewCell.h"

@interface ECMemberCommonSelectViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) UIWindow * effectWindow;

@property (nonatomic,strong) UIButton * maleBT;

@property (nonatomic,strong) UIButton * femaleBT;

@property (nonatomic,copy) NSIndexPath * selectedIndexPath;

@end

@implementation ECMemberCommonSelectViewController

- (void)viewDidLoad {
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:nil];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)loadSubView {
    WS(weakSelf, self);
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.right.mas_equalTo(weakSelf.view);
        if (@available(iOS 11.0,*)) {
            make.bottom.mas_equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        }
    }];
}

- (void)doSelect:(UIButton*)sender {
    NSInteger row = self.selectedIndexPath.row;
    self.dataArr[row][@"value"] = sender.titleLabel.text;
    [self.effectWindow setHidden:YES];
    [self.effectWindow resignKeyWindow];
    [self.tableView reloadRowAtIndexPath:self.selectedIndexPath withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark -UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ECMemberAddTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSMutableDictionary * dic = self.dataArr[indexPath.row];
    cell.isNeed = [[dic objectForKey:@"isNeed"] boolValue];
    cell.cellStyle = [[dic objectForKey:@"style"] integerValue];
    cell.title = [dic objectForKey:@"title"];
    switch (cell.cellStyle) {
        case ECMemberAddCellStyleTxt:
        {
            if (!dic[@"value"] || ((NSString*)dic[@"value"]).length < 1) {
                cell.detailTF.placeholder = dic[@"placeholder"];
            }else{
                cell.detailTF.text = dic[@"value"];
            }
        }
            break;
        case ECMemberAddCellStyleMore: {
            if (!dic[@"value"] || ((NSString*)dic[@"value"]).length < 1) {
                cell.detailLB.text = dic[@"placeholder"];
                cell.detailLB.textColor = ECCOLOR(0xcccccc);
            }else{
                cell.detailLB.text = dic[@"value"];
                cell.detailLB.textColor = ECCOLOR(0x333333);
            }
        }
            break;
        case ECMemberAddCellStyleSwitch:{
            if ([dic[@"value"] boolValue]) {
                cell.isON = YES;
            }else {
                cell.isON = NO;
            }
        }
            break;
        default:
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary * dic = self.dataArr[indexPath.row];
    if ([dic[@"title"] isEqualToString:@"性别"]) {
        self.effectWindow.hidden = NO;
        [self.effectWindow makeKeyAndVisible];
        self.selectedIndexPath = indexPath;
    }
}

#pragma mark -lazyLoad

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.00000001)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.00000001)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionHeaderHeight = 0.00001 * PX_RELATIVE375;
        _tableView.sectionFooterHeight = 0.00001 * PX_RELATIVE375;
        [_tableView registerClass:[ECMemberAddTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
    }
    return _tableView;
}

- (UIWindow *)effectWindow {
    if (!_effectWindow) {
        _effectWindow = [UIWindow new];
        WS(weakWindow, _effectWindow);
        _effectWindow.backgroundColor = [ECCOLOR(0x010101) colorWithAlphaComponent:0.62];
        UIView * selectView = [UIView new];
        selectView.backgroundColor = ECCOLOR(0xffffff);
        self.maleBT = [self creatCommonTB:@"男"];
        [selectView addSubview:self.maleBT];
        self.maleBT.frame = CGRectMake(0, 0, ECSCREEN_WIDTH, 44);
        
        CAShapeLayer * line = [CAShapeLayer layer];
        line.borderWidth = 0.5;
        line.borderColor = ECCOLOR(0xD2d2d2).CGColor;
        line.frame = CGRectMake(0, 44, ECSCREEN_WIDTH, 1);
        [selectView.layer addSublayer:line];
        
        self.femaleBT = [self creatCommonTB:@"女"];
        [selectView addSubview:self.femaleBT];
        self.femaleBT.frame = CGRectMake(0, 45, ECSCREEN_WIDTH, 44);
        
        [_effectWindow addSubview:selectView];
        [selectView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.mas_equalTo(weakWindow);
            if (@available(iOS 11.0,*)) {
                make.top.mas_equalTo(weakWindow.mas_safeAreaLayoutGuideBottom).mas_offset(-89);
            }else {
                make.top.mas_equalTo(weakWindow.mas_bottom).mas_offset(-89);
            }
        }];
        [_effectWindow addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            weakWindow.hidden = YES;
            [weakWindow resignKeyWindow];
        }]];
    }
    return _effectWindow;
}

- (UIButton*)creatCommonTB:(NSString*)txt {
    UIButton * bt = [UIButton buttonWithType:UIButtonTypeCustom];
    [bt setTitle:txt forState:UIControlStateNormal];
    [bt setTitleColor:ECCOLOR(0x333333) forState:UIControlStateNormal];
    bt.titleLabel.font = ECFONT(18);
    [bt addTarget:self action:@selector(doSelect:) forControlEvents:UIControlEventTouchUpInside];
    return bt;
}

@end
